var express = require('express');
var router = express.Router();
var connect = require('../public/javascripts/connect');

var result = {};
router.get('/',async function(req, res, next) {
    var data = [],num = [];
    for (let i = 0; i < 8; i++) {
        let datas = [],nums = [];
        for (let j = 0; j < 3; j++) {
            let value1;
            let sql1 = `SELECT COUNT(股份数量) FROM gudongtoupiao WHERE 第${i+1}个议案=${j}`;
            await new Promise((res1) => {
                connect.connection3.query(sql1, function (err, result2, fields) {
                    if (err) {
                        console.log('[SELECT ERROR] - ', err.message);
                        return;
                    }
                    value1 = result2[0];
                    res1(true);
                })
            })
            let value;
            let sql = `SELECT SUM(股份数量) FROM gudongtoupiao WHERE 第${i+1}个议案=${j}`;
            await new Promise((res1) => {
                connect.connection3.query(sql, function (err, result1, fields) {
                    if (err) {
                        console.log('[SELECT ERROR] - ', err.message);
                        return;
                    }
                    value = result1[0];
                    res1(true);
                })
            })
            nums.push(value1);
            datas.push(value);
        }
        num.push(nums);
        data.push(datas);
    }
    result.num = JSON.stringify(num)
    result.data = JSON.stringify(data);
    console.log("result:", result);
    res.render('tongji', {result});
});

module.exports = router;
